<?php
// +----------------------------------------------------------------------
// | 海豚PHP框架 [ DolphinPHP ]
// +----------------------------------------------------------------------
// | 版权所有 2016~2017 河源市卓锐科技有限公司 [ http://www.zrthink.com ]
// +----------------------------------------------------------------------
// | 官方网站: http://dolphinphp.com
// +----------------------------------------------------------------------
// | 开源协议 ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------

namespace app\shop\admin;

use app\admin\controller\Admin;
use app\common\builder\ZBuilder;
use think\Db;

/**             
 * 行为管理控制器
 * @package app\admin\controller
 */
class Shopbank extends Admin
{
    public function index()
    {
        cookie('__forward__', $_SERVER['REQUEST_URI']);
        $map = $this->getMap();
        // 数据列表
        $data_list = Db::name('shop_settlement')->where($map)->order('bankid desc')->paginate();

        // 分页数据
        $page = $data_list->render();

        // 使用ZBuilder快速创建数据表格
        return ZBuilder::make('table')
            ->setPageTitle('店铺银行信息')// 设置页面标题
            ->setPrimaryKey('bankid')
            ->setTableName('shop_settlement')// 设置数据表名
            ->setSearch(['account_name' => '银行开户名','bank_name'=>'银行名称'])// 设置搜索参数
            ->addColumns([ // 批量添加列
                ['bankid', '银行编号','','','','fix-table-td'],
                ['shopid', '店铺编号','','','','fix-table-td'],
                ['account_name', '银行开户名','','','','fix-table-td'],
                ['bank_no', '公司银行账户','','','','fix-table-td'],
                ['bank_name', '开户银行支行名称','','','','fix-table-td'],
                ['subbranch_id', '支行联行号','','','','fix-table-td'],
                ['band_addr', '开户银行所在地','','','','fix-table-td'],
                ['right_button', '操作', 'btn']
            ])
            ->addTopButtons('add,delete')// 批量添加顶部按钮
            ->addRightButtons('edit')// 批量添加右侧按钮
            ->addRightButton('delete',['icon' => 'fa fa-times', 'title' => '删除', 'href' => url('delete_bank', ['bankid' => '__id__'])])
            ->setRowList($data_list)// 设置表格数据
            ->setPages($page)// 设置分页数据
            ->fetch(); // 渲染页面
    }

    //新增银行
    public function add()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            // 验证
            $result = $this->validate($data, 'ShopSettlement');
            // 验证失败 输出错误信息
            if (true !== $result) {
                return $this->error($result);
            }
            $shop=$data['shopid'][0];
            $data['shopid']=$shop;
            $data['createtime']=time();
            $data['updatetime']=time();
            $res=Db::name('shop_settlement')->insert($data);
            if ($res) {
                return $this->success('新增成功', cookie('__forward__'));
            } else {
                return $this->error('新增失败');
            }
        }
        // 使用ZBuilder快速创建表单
        return ZBuilder::make('form')
            ->setPageTitle('新增银行信息')// 设置页面标题
            ->addSelect('shopid','选择店铺','','','','ajax','',['url'=>'/admin.php/Shop/Shopbank/shoplist','datakey'=>'id','dataname'=>'name'])
            ->addFormItems([ // 批量添加表单项
                ['text', 'account_name', '银行开户名', ''],
                ['number', 'bank_no', '公司银行账户',''],
                ['text', 'bank_name', '开户银行支行名称', ''],
                ['number', 'subbranch_id', '支行联行号', ''],
                ['text', 'band_addr', '开户银行所在地', ''],
            ])
            ->layout(['account_name'=>4,'bank_no'=>4,'bank_name'=>4,'subbranch_id'=>4,'band_addr'=>4,'shopid'=>4])
            ->fetch();
    }
    //编辑银行
    public function edit($id =''){
        $info = Db::name('shop_settlement')->where('bankid',$id)->find();
        $shopinfo=Db::name("shop")->where("id",$info['shopid'])->find();
        $arr = [];
        $arr[$shopinfo['id']] = $shopinfo['name'];
        $testid = $shopinfo['id'];
        if ($this->request->isPost()) {
            $data = $this->request->post();
            // 验证
            $result = $this->validate($data, 'ShopSettlement');
            // 验证失败 输出错误信息
            if (true !== $result) {
                return $this->error($result);
            }
            $shop=$data['shopid'][0];
            $data['shopid']=$shop;
            $data['updatetime']=time();
            $res=Db::name('shop_settlement')->update($data);
            if ($res) {
                return $this->success('编辑成功', cookie('__forward__'));
            } else {
                return $this->error('编辑失败');
            }
        }

        // 使用ZBuilder快速创建表单
        return ZBuilder::make('form')
            ->setPageTitle('店铺银行编辑')// 设置页面标题
            ->addFormItems([ // 批量添加表单项
                ['hidden','bankid'],
                ['select','shopid','选择会员','',$arr,$testid,'ajax','',['url'=>'/admin.php/Shop/Shopbank/shoplist','datakey'=>'id','dataname'=>'name']],
                ['text', 'account_name', '银行开户名', ''],
                ['number', 'bank_no', '公司银行账户',''],
                ['text', 'bank_name', '开户银行支行名称', ''],
                ['number', 'subbranch_id', '支行联行号', ''],
                ['text', 'band_addr', '开户银行所在地', ''],
            ])
            ->layout(['account_name'=>4,'bank_no'=>4,'bank_name'=>4,'subbranch_id'=>4,'band_addr'=>4,'shopid'=>4])
            ->setFormData($info) // 设置表单数据
            ->fetch();
    }
    //单个删除
    public function delete_bank(){
        $id = $this->request->param('bankid');
        $res=Db::name("shop_settlement")->where('bankid',$id)->delete();
        if ($res) {
            return $this->success('删除成功',cookie('__forward__'));
        } else {
            return $this->error('删除失败');
        }
    }
    //批量删除
    public function delete($record = [])
    {
        $ids        = $this->request->isPost() ? input('post.ids/a') : input('param.ids');
        foreach ($ids as $values){
            $res=Db::name("shop_settlement")->where('bankid',$values)->delete();
        }
        if ($res) {
            return $this->success('删除成功',cookie('__forward__'));
        } else {
            return $this->error('删除失败');
        }
    }
    //选择店铺列表
    public function shoplist(){
        $search=input('q');
        $where['name']=['like',"%$search%"];
//        $where['status']=1;
//        $where['is_enable']=1;
        $shop=Db::name('shop')->field('id,name')->where($where)->select();
        $shoplist=[];
        $a= 0;
        foreach($shop as $value){
            $b = $a++;
            $key=$value['id'];
            $shoplist[$b]['id']=$key;
            $shoplist[$b]['name']=$value['name'];
        }
        return json(['items'=>$shoplist,'total_count'=>sizeof($shoplist)]);
    }
}